<template>
	<div ref="chartRef" :style="{ width, height }"></div>
</template>
<script lang="ts" setup>
import { Ref, ref } from 'vue';
import { useECharts } from '/@/hooks/web/useECharts';
import type { PropType } from 'vue';
import { alarmAcknowledgement } from '/@/api/analysis/index';
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

const props = defineProps({
	width: {
		type: String as PropType<string>,
		default: '100%',
	},
	height: {
		type: String as PropType<string>,
		default: '100%',
	},
	color: {
		type: Array as PropType<string[]>,
		default: () => ['#00c997', '#4c84ff'],
	},
});

const chartRef = ref<HTMLDivElement | null>(null);
const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
const getAlarmAcknowledgement = () => {
	alarmAcknowledgement().then((res) => {
		let total = 0,
			data = [];
		for (const k in res.data) {
			data.push({
				name: k,
				value: res.data[k],
			});
			total += parseInt(res.data[k]);
		}
		setOptions({
			title: {
				text: t('analysis.AlarmConfirmationType'),
				textStyle: { color: '#3ecafc', fontWeight: 'normal' },
			},
			tooltip: {
				trigger: 'item',
				position: 'top',
				formatter: '{b}: {d}%<br /> {c}',
			},
			legend: {
				show: true,
				align: 'left',
				right: 20,
				icon: 'circle',
				top: 10,
				padding: 0,
				itemWidth: 10,
				textStyle: {
					color: '#fff',
					fontSize: 10,
				},
			},
			grid: { left: '2%', top: 50, bottom: 10, containLabel: true },
			graphic: [
				{
					type: 'text',
					top: '51%',
					left: 'center',
					style: {
						text: `${total}`,
						fontSize: 18,
						fontWeight: 700,
						fill: '#fff',
					},
					tooltip: {
						show: false,
					},
				},
			],
			series: [
				{
					type: 'pie',
					z: 999,
					radius: ['50%', '66%'],
					center: ['50%', '55%'],
					color: props.color,
					minAngle: 2,
					label: {
						show: true,
						formatter: '{c}\n{d}%',
						color: '#d8d8d8',
					},
					data: data,
					labelLine: {
						show: true,
					},
					animationType: 'scale',
					animationEasing: 'exponentialInOut',
					animationDelay: function () {
						return Math.random() * 400;
					},
				},
			],
		});
	});
};
onMounted(() => {
	getAlarmAcknowledgement();
});
</script>
